package com.haoxi.gateway.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.GatewayInfo;
import com.haoxi.gateway.common.InitGatewayConfig;
import com.haoxi.gateway.common.InitSystemControlConfig;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.runner.specialmobile.SpecialMobileMrg;
import com.haoxi.subdsmp.SubDSMP;
import com.haoxi.subdsmp.SubDSMPTimer;

/**
 * 系统主控程序
 * 
 * Revision History
 * 
 * 2006-6-3,dean.xu,created it
 */
public class MainSystem {
    private static Log log = LogFactory.getLog(MainSystem.class);
    /**
     * 当前运行的网关的gatewayCode
     */
    private static String gatewayCode;
    /**
     * @return Returns the gatewayCode.
     */
    public static String getGatewayCode() {
        return MainSystem.gatewayCode;
    }
    /**
     * @param gatewayCode The gatewayCode to set.
     */
    public static void setGatewayCode(String gatewayCode) {
        MainSystem.gatewayCode = gatewayCode;
    }
    /**
     * 系统启动函数
     * 
     * @param systemName
     */
    private void startSystem(String systemName) {
        log.debug("====Startup the " + systemName + " Gateway system start！====");
        // 初始系统参数
        RunnerMgr mgr = RunnerMgr.getInstance();
        // 加载网关基本信息
        try {
            log.debug("----" + systemName + " start load the gateway Config info!----");
            InitGatewayConfig.getInstance().initGatewayConfig();
            GatewayInfo gatewayInfo = InitGatewayConfig.getInstance().getGatewayInfo();
            mgr.setGatewayInfo(gatewayInfo);
            log.debug("----" + systemName + " load the gateway Config info successfully!----");
        } catch (Throwable e) {
            log.error("----" + systemName + " load the gateway Config info unsuccessfully!----", e);
            System.exit(1);
        }

        // 加载系统配置信息
        try {
            log.debug("----" + systemName + " start load system control_config info!----");
            mgr.setSystemConf(InitSystemControlConfig.loadSystemControlConfig());
            log.debug("----" + systemName + " load system control_config info successfully!----");
        } catch (Exception e) {
            log.error("----" + systemName + " load system control_config info unsuccessfully!----", e);
            System.exit(1);
        }

        // 加载系统——内存数据保护信息
         try {
             log.debug(systemName+" start set memorydata protecting!-------");
             mgr.setMemorydata_protect(InitSystemControlConfig.getInt(SYSConst.CONF_MEMORYDATA_PROTECT));
             log.debug("-----"+systemName+ " set memorydata protecting successfully!-------");
         } catch (Exception e) {
             log.error("-----"+systemName+ " set memorydata protecting unsuccessfully!-------",e);
             System.exit(1);
         }
        // 屏蔽 by dean
        // 初始黑名单和黑名单
//         if (!SpecialMobileMrg.init()){
//         log.error("-----"+systemName+
//         " init the special mobile info failure, Please check the system and reboot it！--------"
//         );
//         // System.exit(1);
//         }else{
//         log.debug("-----"+systemName+
//         " init the special mobile info successfully！--------");
//         log.debug ( "SenseMobileMap length is: [" +
//         SpecialMobileMrg.senseMobileMap.size () + "]" );
//         log.debug ( "BlackMobileMap length is: [" +
//         SpecialMobileMrg.blackMobileMap.size () + "]" );
//         log.debug ( "BlackLongNumMap length is: [" +
//         SpecialMobileMrg.blackLongNumMap.size () + "]" );
//         }
        // 屏蔽 end
        // 启动系统
        log.debug("------" + systemName + " startup the process runner!--------");
        if (!mgr.startRunner()) {
            log.error("------" + systemName + " startup the process runner unsucessfully!--------");
            System.exit(1);
        }
        log.debug("------" + systemName + " startup the process runner sucessfully!--------");
        log.debug("----------Startup the " + systemName + " Gateway system successfully！--------------------");
    }

    /**
     * 整个系统主启动程序
     * 
     * @param args
     */
    public static void main(String args[]) {

        MainSystem m = new MainSystem();
        
        m.startSystem("GatewaySystem");
    }

}
